package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ityu
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlan nursingPlan = this.getById(id);

        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> list2 = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo bean = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            bean.setProjectId(nursingProjectPlan.getProjectId().toString());
            bean.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return bean;
        }).toList();
        NursingPlanVo result = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        result.setProjectPlans(list2);
        return result;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        return this.save(nursingPlan) == true?1:0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        return this.updateById(nursingPlan) == true?1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return  this.removeByIds(Arrays.asList(ids)) == true?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        return this.removeById(id) == true?1:0;
    }

    @Override
    @Transactional
    public int saveNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlanList = dto.getProjectPlans();

        Boolean b = true;
        if (!CollectionUtils.isEmpty(nursingProjectPlanList)){
            nursingProjectPlanList.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(Long.parseLong(nursingPlan.getId().toString()));
            });
            b = nursingProjectPlanService.saveBatch(nursingProjectPlanList);
        }
        return b ? 1:0;
    }
}
